from django.core.paginator import Paginator
from django.http import HttpResponse, HttpRequest, HttpResponseRedirect
from django.shortcuts import render_to_response
from looptube.models import VideoInfo, Playlist, RadioLog
from looptube.utils import digg_paginator
from . import commonInit

def init(request):
    # init from commonInit()
    resp = commonInit(request)
    if (HttpResponse == resp.__class__):
        return resp     # commonInit() returned HttpResponse
    contextDict = resp  # commonInit() returned dict
            
    return contextDict

# actions

def videoListAction(request, type, channelName=None, page=1):
    numPerPage = 50
    page = int(page)
    
    #init from init()
    resp = init(request)
    if (HttpResponse == resp.__class__):
        return resp     # init() returned HttpReponse
    contextDict = resp  # init() returned dict

    if ('channel' == type):
        channelDict = contextDict['channelDict']
        if (not channelDict.has_key(channelName)):
            return HttpResponseRedirect("/")

    startIndex = (page-1) * numPerPage
    if ('channel' != type):
        videoNum = VideoInfo.all().count()
        if (videoNum > 1000):
            videoNum = 1000;
        if (startIndex >= videoNum):
            return HttpResponseRedirect('/')
    
        if ('top' == type):
            videoInfos = VideoInfo.getTopVideos(numPerPage, startIndex)
            contextDict['pageTitle'] = 'Top Videos'
            pathPrefix = 'topvideos'
        elif ('recent' == type):
            videoInfos = VideoInfo.getRecentVideos(numPerPage, startIndex)
            contextDict['pageTitle'] = 'Recently Looped Videos'
            pathPrefix = 'recentvideos'
        else:
            return HttpResponseRedirect('/')
    else:
        radioName = channelDict[channelName]
        videoNum = RadioLog.getNumRadioRecentVideoInfos(radioName)
        if (videoNum > 1000):
            videoNum = 1000;
        if (startIndex >= videoNum):
            return HttpResponseRedirect('/')
        videoInfos = RadioLog.getRadioRecentVideoInfos(radioName, numPerPage, startIndex)
        
        channelNameDict = contextDict['channelNameDict']
        contextDict['pageTitle'] = _('Channel Recently Added Videos') + ': ' + channelNameDict[channelName]
        contextDict['breadcrumbs'].append({ 'name' : _('Channels'), 'url' : '/channel' })        
        pathPrefix = 'channel/recent/%s' % channelName
    paginator = Paginator(range(0, videoNum), numPerPage)


    contextDict['videoInfos'] = videoInfos
    contextDict['digg_paginator'] = digg_paginator(paginator, page, numPerPage, ('/%s/' % (pathPrefix)))

    return render_to_response('videoinfos.html', contextDict)

def indexAction(request):
    #init from init()
    resp = init(request)
    if (HttpResponse == resp.__class__):
        return resp     # init() returned HttpReponse
    contextDict = resp  # init() returned dict

    contextDict['pageTitle'] = ''
    contextDict['topVideoInfos'] = VideoInfo.getTopVideos(6)
    contextDict['recentVideoInfos'] = VideoInfo.getRecentVideos(6)
    contextDict['topPlaylists'] = Playlist.getTopPlaylists(10)
    contextDict['recentCreatedPlaylists'] = Playlist.getRecentCreatedPlaylists(10)
    
    return render_to_response('index.html', contextDict)
